package com.ljf.interestingmaps.routefinder;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlacePicker;
import com.ljf.interestingmaps.R;

public class RouteActivity extends AppCompatActivity implements View.OnClickListener,GoogleApiClient.OnConnectionFailedListener{

    public static final int PLACE_PICKER_REQUEST1 = 1;
    public static final int PLACE_PICKER_REQUEST2 = 2;

    private View searchBtn;

    private View souceLayout;
    private View destinationLayout;
    private TextView sourceTv;
    private TextView detionationTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_route_live);
        searchBtn = findViewById(R.id.searchBtn);
        searchBtn.setOnClickListener(this);

        souceLayout = findViewById(R.id.souceLayout);
        destinationLayout = findViewById(R.id.destinationLayout);
        sourceTv = (TextView) findViewById(R.id.sourceTv);
        detionationTv = (TextView) findViewById(R.id.detionationTv);
        souceLayout.setOnClickListener(this);
        destinationLayout.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        if(v == searchBtn){

            String strFrom = sourceTv.getText().toString();
            String strTo = detionationTv.getText().toString();
            String uri = "http://maps.google.com/maps?f=d&saddr="+ strFrom +"&daddr=" + strTo +"&hl=en";
            Log.i("RouteSample","uri=" + uri);


            Intent mapIntent =new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
            mapIntent.setPackage("com.google.android.apps.maps");
            startActivity(mapIntent);

        }else if( v == souceLayout){
            try {

                PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();

                startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST1);
            } catch (GooglePlayServicesRepairableException e) {
                // TODO: Handle the error.
            } catch (GooglePlayServicesNotAvailableException e) {
                // TODO: Handle the error.
            }
        }else if(v == destinationLayout){
            try {

                PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();

                startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST2);
            } catch (GooglePlayServicesRepairableException e) {
                // TODO: Handle the error.
            } catch (GooglePlayServicesNotAvailableException e) {
                // TODO: Handle the error.
            }
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == PLACE_PICKER_REQUEST1) {
            if (resultCode == RESULT_OK) {
                Place place = PlacePicker.getPlace(data, this);
                sourceTv.setText(place.getName());
            }
        }else if (requestCode == PLACE_PICKER_REQUEST2) {
            if (resultCode == RESULT_OK) {
                Place place = PlacePicker.getPlace(data, this);
                detionationTv.setText(place.getName());
            }
        }
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        Log.e("peace","onConnectionFailed   " + connectionResult.toString());
    }
}
